/**
 * @file Parameter.cpp
 * @author RittoQin (1264145445@qq.com)
 * @brief 
 * @version 1.0
 * @date 2022/4/18
 *
 * @copyright Copyright SCUT RobotLab(c) 2022
 *
 */

#include <fstream>
#include <opencv2/core/persistence.hpp>
#include "Parameter.h"

using namespace std;
using namespace cv;

Parameter::Parameter(const string &path) {
    FileStorage p(path, FileStorage::READ);

    p["MODEL"] >> this->MODEL;
    p["INPUT_SIZE"] >> this->INPUT_SIZE;
    p["NUM_CLASSES"] >> this->NUM_CLASSES;
    p["OBJ_THRESH"] >> this->OBJ_THRESH;
    p["NMS_THRESH"] >> this->NMS_THRESH;
    p["SCORE_THRESH"] >> this->SCORE_THRESH;
    // output layers' strides
    vector<int> strides;
    p["STRIDES"] >> strides;
    this->STRIDES = strides;
    // output layers' anchors
    vector<int> anchors_s32, anchors_s16, anchors_s8;
    p["ANCHORS_S32"] >> anchors_s32;
    p["ANCHORS_S16"] >> anchors_s16;
    p["ANCHORS_S8"] >> anchors_s8;
    // 先验框尺寸应为6
    assert(anchors_s32.size() == 6 && anchors_s16.size() == 6 && anchors_s8.size() == 6);
    // write anchors
    this->ANCHORS_S32.resize(3);
    this->ANCHORS_S32 = vector<Anchor>{Anchor{anchors_s32[0], anchors_s32[1]}, Anchor{anchors_s32[2], anchors_s32[3]},
                                       Anchor{anchors_s32[4], anchors_s32[5]}};
    this->ANCHORS_S16.resize(3);
    this->ANCHORS_S16 = vector<Anchor>{Anchor{anchors_s16[0], anchors_s16[1]}, Anchor{anchors_s16[2], anchors_s16[3]},
                                       Anchor{anchors_s16[4], anchors_s16[5]}};
    this->ANCHORS_S8.resize(3);
    this->ANCHORS_S8 = vector<Anchor>{Anchor{anchors_s8[0], anchors_s8[1]}, Anchor{anchors_s8[2], anchors_s8[3]},
                                      Anchor{anchors_s8[4], anchors_s8[5]}};
    // 读取txt文本文件
    ifstream infile;
    infile.open("../cfg/class.txt");
    string buf;
    // classes name
    while (getline(infile, buf)) this->CLASSES.emplace_back(buf);
    // output layers' name
    ifstream infile1;
    infile1.open("../cfg/output.txt");
    while (getline(infile1, buf)) this->LAYER_NAMES.emplace_back(buf);
}

Parameter param("/home/qyt/桌面/vision/Engineer/MNN_test/cfg/ModelParam.yaml");


